package com.o2o.intercepitor;


import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.StrutsStatics;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.o2o.content.SessionContent;
import com.o2o.model.user.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class UserInSessionInterceptior implements Interceptor {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2764928528713887242L;

	Logger logger = LoggerFactory.getLogger(this.getClass());
	
	public void destroy() {
		logger.info("********userIntercepitor destroy**********");
	}

	public void init() {
		logger.info("*********userIntercepitor init************");

	}

	public String intercept(ActionInvocation invoc) throws Exception {
	User user = (User)invoc.getInvocationContext().getSession().get(SessionContent.NORMAL_USER_SESSION);
			if (user != null) {
				return invoc.invoke();
			} else{
				Map<String, Object> session = invoc.getInvocationContext().getSession();
				HttpServletRequest request= (HttpServletRequest)  invoc.getInvocationContext().get(StrutsStatics.HTTP_REQUEST);
				String query = request.getQueryString();
				String requestURL =  (query == null ?request.getServletPath() :  request.getServletPath() + "?" + query );
//				System.out.println("request:"+requestURL);
//				System.out.println("ServletPath:"+ request.getServletPath());
//				System.out.println("ContextPath:"+ request.getContextPath());
//				invoc.getInvocationContext();
				ActionContext.getContext().put("redirect", requestURL);
				session.put(SessionContent.REDIRECT_URL, requestURL);
				return "need_login";
			}
	} 

}
